﻿using UnityEngine;

public class Player : MonoBehaviour
{
    #region Variables & Objects
    private const float MOVEMENTSPEED = 5f;
    private bool _playerOneCanAttackPlayerTwo = false;
    private bool _playerTwoCanAttackPlayerOne = false;
    private Animator anim;

    public KeyboardLayoutEnum _keyboardLayout;
    public float health = 1000;
    #endregion

    #region Methods

    private void Start()
    {
        this.anim = GetComponent<Animator>();
    }

    private void Update()
    {
        this.KeyboardInput();

        if (this.health <= 0)
        {
            Destroy(this.gameObject);
        }

        if (Input.GetKey(KeyCode.F1))
        {
            this.anim.Play("SwingSword");
        }

        if (this._playerOneCanAttackPlayerTwo && Input.GetKey(KeyCode.F1))
        {
            var obj = GameObject.FindGameObjectWithTag("Player2");
            Destroy(obj);
        }
        if (this._playerTwoCanAttackPlayerOne && Input.GetKey(KeyCode.KeypadEnter))
        {
            var obj = GameObject.FindGameObjectWithTag("Player1");
            Destroy(obj);
        }
    }

    private void KeyboardInput()
    {
        switch (this._keyboardLayout)
        {
            case KeyboardLayoutEnum.None:
                throw new KeyboardLayoutNotSelectedException("A player object was used but no keyboard layout was selected.");
            case KeyboardLayoutEnum.LayoutLeft:
                this.PlayerMovement(KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D);
                return;
            case KeyboardLayoutEnum.LayoutRight:
                this.PlayerMovement(KeyCode.UpArrow, KeyCode.LeftArrow, KeyCode.DownArrow, KeyCode.RightArrow);
                return;
            default:
                throw new KeyboardLayoutNotFoundException("No keyboard layout found for {0}.".FormatString(this._keyboardLayout.ToString()));
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player2")
            this._playerOneCanAttackPlayerTwo = true;
        if (other.gameObject.tag == "Player1")
            this._playerTwoCanAttackPlayerOne = true;
    }

    void OnTriggerExit2D(Collider2D other)
    {
        this._playerOneCanAttackPlayerTwo = false;
        this._playerTwoCanAttackPlayerOne = false;
    }

    private void PlayerMovement(KeyCode up, KeyCode left, KeyCode down, KeyCode right)
    {
        if (Input.GetKey(up))   
            this.rigidbody2D.velocity = new Vector2(0, MOVEMENTSPEED);
        else if (Input.GetKey(left)) 
            this.rigidbody2D.velocity = new Vector2(MOVEMENTSPEED * -1, 0);
        else if (Input.GetKey(down)) 
            this.rigidbody2D.velocity = new Vector2(0, MOVEMENTSPEED * -1);
        else if (Input.GetKey(right))
            this.rigidbody2D.velocity = new Vector2(MOVEMENTSPEED, 0);
        else
            this.rigidbody2D.velocity = new Vector2(0, 0);       
    }

    #endregion
}